/*
 * Copyright (c) 2017. 深圳一七网络科技. All rights reserved.
 */

package com.suozhang.framework.utils;

import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;

/**
 *      LIJUWEN
 * @email yiyayiyayaoljw@gmail.com
 * @date 2017/4/12 16:29
 */
public class StringUtil {

    // 使用String的split 方法
    public static String[] convertStrToArray(String str) {
        String[] strArray = null;
        strArray = str.split(","); // 拆分字符为"," ,然后把结果交给数组strArray
        return strArray;
    }

    /**
     * 改变TextView部分字体的颜色和大小
     *
     * @param originalValue
     * @param indexValue
     * @return
     */
    public static SpannableStringBuilder setTextViewColor(String originalValue, String indexValue, int color) {
        SpannableStringBuilder builder = new SpannableStringBuilder(originalValue);
        if (TextUtils.isEmpty(originalValue)) {
            return builder;
        }
        for (int j = 0; j < originalValue.length(); j++) {
            if (j + indexValue.length() <= originalValue.length()) {
                String name = (String) originalValue.subSequence(j, j + indexValue.length());
                if (name.equals(indexValue)) {
                    ForegroundColorSpan redSpan = new ForegroundColorSpan(color);
                    builder.setSpan(redSpan, j, j + indexValue.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }
        }
        return builder;
    }
}
